<?php
namespace app\mobile\controller;
use app\api\controller\Pub;

use app\common\model\Agent;
use app\common\model\CardMember;
use app\common\model\CardQrcode;
use app\common\model\CardUserGet;
use app\common\model\Merchant;
use think\Request;
use think\Session;
class Member extends Pub
{

    //获取卡券类型
    public function getcardtype($type)
    {
        swiTch ($type) {
            case 'GROUPON':
                $rest = "团购券";
                return $rest;
                break;
            case 'CASH':
                $rest = "代金券";
                return $rest;
                break;
            case 'DISCOUNT':
                $rest = "折扣券";
                return $rest;
                break;
            case 'GIFT':
                $rest = "兑换券";
                return $rest;
                break;
            case 'GENERAL_COUPON':
                $rest = "优惠券";
                return $rest;
                break;
            case 'GENERAL_CARD':
                $rest = "礼品卡";
                return $rest;
                break;
        }

    }

    //会员卡券列表
    public function member_cards_list($param)
    {
        try {
            $CardUserGet = new CardUserGet();
            $map['a.mid'] = ['in', $param['merchantid']];
            $map['a.openid'] = $param['openid'];
            $list = $CardUserGet->alias('a')->join('wn_card_general b')->where($map)->field('a.id,a.card_id,a.code,a.card_type,a.create_time,b.title,b.brand_name')->select();
            if ($list) {
                foreach ($list as $k => $v) {
                    $list[$k]['card_type'] = $this->getcardtype($v['card_type']);
                    $list[$k]['code'] = $v['code'];
                }
                $data['return_code'] = "SUCCESS";
                $data['return_msg'] = "SUCCESS";
                $data['result_code'] = "SUCCESS";
                $data['result_msg'] = "SUCCESS";
                $data['info'] = $list;
            } else {
                $data = array("return_code" => "SUCCESS", "return_msg" => "SUCCESS", "result_code" => "FAIL", "result_msg" => "此会员无卡券",);
            }
        } catch (\Exception $e) {
            $data = array("return_code" => "FAIL", "return_msg" => "系统错误", "result_code" => "FAIL", "result_msg" => $e->getMessage());
        }
        return $data;
    }

    //门店详情
    public function getmachinfo($param)
    {
        try {
            $Merchant = new Merchant();
            $map['id'] = $param['merchantid'];
            $machinfo = $Merchant->where($map)->field('id,img,merchant,agent,top_mid')->find();
            if ($machinfo) {
                $Agent = new Agent();
                $agentinfo = $Agent->where(array('id' => $machinfo['agent']))->field('name,contacts,tel,open_url')->find();
                $CardMember = new CardMember();
                $cardid = $CardMember->where(['top_mid' => $machinfo['top_mid']])->value('card_id');
                $CardQrcode = new CardQrcode();
                $qrcode_url = $CardQrcode->where(['card_id' => $cardid, 'mid' => $param['merchantid']])->value('qrcode_url');
                $re_data['mch_img'] = $machinfo['img'];
                $re_data['mch_id'] = $machinfo['id'];
                $re_data['mch_name'] = $machinfo['merchant'];
                $re_data['mch_card_url'] = $qrcode_url;
                $re_data['mch_pay_url'] = "https://" . $agentinfo['open_url'] . "/qrcode/" . $machinfo['id'] . '.png';;
                $re_data['agent_name'] = $agentinfo['name'];
                $re_data['agent_contact'] = $agentinfo['contacts'];
                $re_data['agent_tel'] = $agentinfo['tel'];
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
                $re_param['result_code'] = "SUCCESS";
                $re_param['result_msg'] = "SUCCESS";
                $re_param['return_code'] = "SUCCESS";
                $re_param['info'] = $re_data;
            } else {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "SUCCESS";
                $re_param['result_code'] = "FAIL";
                $re_param['result_msg'] = "未查询到商户信息";
                $re_param['return_code'] = "";
            }
        } catch (\Exception $e) {
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = '系统错误';
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '系统错误' . $e->getMessage();
        }
        if ($qrcode_url) {
            $rarr['code'] = 0;
            $rarr['message'] = 'SUCCESS';
            $rarr['qrcode_url'] = $qrcode_url;
        } else {
            $rarr['code'] = 9091;
            $rarr['message'] = 'ERROR';
        }
        return $re_param;

    }

}
